package edu.depaul.se.courseregistration.sessionbean;

import edu.depaul.se.courseregistration.jpa.courses.College;
import edu.depaul.se.courseregistration.jpa.courses.Course;
import edu.depaul.se.courseregistration.jpa.courses.Section;
import edu.depaul.se.courseregistration.jpa.courses.Subject;
import java.util.List;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author Group2
 */

@Stateless
@LocalBean
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public class CourseEJB {

    @PersistenceContext(unitName="CourseRegistrationModulePU")
    private EntityManager em;
    
    //Courses buisness APIs
    //@Override
    public List<Course> getAllCourse() {
        return em.createNamedQuery(Course.COURSE_QUERY_ALL).getResultList();
    }
     
    //@Override
     public List<Course> findCourseBySubject(int subjectId) {
        Subject subject = em.find(Subject.class, subjectId); 
        return em.createNamedQuery(Course.COURSE_QUERY_BY_SUBJECT_ID, Course.class).
                    setParameter("subjectID", subject).getResultList();
    }
  
    //@Override
    public Course findCourseById(int courseNumber) {
        return em.createNamedQuery(Course.COURSE_QUERY_BY_ID, Course.class).
                    setParameter("courseNumber", courseNumber).getSingleResult();
    }
    //@Override
    public Course findCourseByTitle(String courseTitle) {
        return em.createNamedQuery(Course.COURSE_QUERY_BY_NAME, Course.class).
                    setParameter("courseTitle", courseTitle).getSingleResult();
    }
  
    //Sections buisness APIs
    
    
    //@Override
    public List<Section> getAllSections() {
        return em.createNamedQuery(Section.SECTION_QUERY_ALL).getResultList();
    }
    //@Override
    public Section findSectionById(int sectionId) {
        return em.createNamedQuery(Section.SECTION_QUERY_BY_ID, Section.class).
                    setParameter("sectionId", sectionId).getSingleResult();
    }
    
    
    //@Override
   public List <Section> findSectionByCourse(int courseNumber) {
        Course course = em.find(Course.class, courseNumber); 
        return em.createNamedQuery(Section.SECTION_QUERY_BY_COURSE_ID, Section.class).
                    setParameter("courseNumber", course).getResultList();
    }
    
    //@Override
    public List <Section> findSectionByIsOpen(boolean isOpen) {
        return em.createNamedQuery(Section.SECTION_QUERY_BY_IS_OPEN, Section.class).
                    setParameter("isOpen", isOpen).getResultList();
    }

    //Subject buisness APIs  
   // @Override
    public List<Subject> getAllSubjects() {
        return em.createNamedQuery(Subject.SUBJECT_QUERY_ALL).getResultList();
    }
    
    //@Override
    public List <Subject> findSubjectByCollege(int collegeId) {//update param type from College to int. Feb.11 -Xiao
        College college = em.find(College.class, collegeId); 
        return em.createNamedQuery(Subject.SUBJECT_QUERY_BY_COLLEGE_ID, Subject.class).
                    setParameter("collegeId", college).getResultList();
    }
    //@Override
    public Subject findSubjectById(int subjectId) {
        return em.createNamedQuery(Subject.SUBJECT_QUERY_BY_ID, Subject.class).
                    setParameter("subjectID", subjectId).getSingleResult();
    }
    
    public Subject findSubjectByCode(String subjectCode) {
        return em.createNamedQuery(Subject.SUBJECT_QUERY_BY_CODE, Subject.class).
                    setParameter("subjectCode", subjectCode).getSingleResult();
    }
    
    //College buisness APIs
    
    public List<College> getAllColleges() {
        return em.createNamedQuery(College.COLLEGE_QUERY_ALL).getResultList();
    }
    
    public College findCollegeById(int collegeId) {
        return em.createNamedQuery(College.COLLEGE_QUERY_BY_ID, College.class).
         setParameter("collegeId", collegeId).getSingleResult();
    }
    
    public College findCollegeByShortName(String collegeShortName) {
        return em.createNamedQuery(College.COLLEGE_QUERY_BY_SHORTNAME, College.class).
                    setParameter("collegeShortName", collegeShortName).getSingleResult();
    }  
}
